#!/bin/bash
#set -e
  sed -i '/fm.check_write_enabled/d'  /home/print3D/moonraker/moonraker/app.py
  sed -i  's/command -v apt/command -v apk/' /home/print3D/moonraker/moonraker/components/update_manager/update_manager.py
  sed -i  's/apt-cache search --names-only/ apk add /' /home/print3D/moonraker/moonraker/components/update_manager/update_manager.py
  sed -i  's/apt list --upgradable/ apk search --update /' /home/print3D/moonraker/moonraker/components/update_manager/update_manager.py
  sed -i  's/install --yest/add/' /home/print3D/moonraker/moonraker/components/update_manager/update_manager.py
  sed -i  's/upgrade --yes/upgrade/' /home/print3D/moonraker/moonraker/components/update_manager/update_manager.py
  grep "max_dbs=MAX_NAMESPACES, lock=False" /home/print3D/moonraker/moonraker/app.py > /dev/null
  if [ $? -ne 0 ]; then
    sed -i  's/max_dbs=MAX_NAMESPACES/&, lock=False/'  /home/print3D/moonraker/moonraker/components/database.py
  fi
  grep "Path not available for DELETE:" /home/print3D/moonraker/moonraker/components/file_manager/file_manager.py > /dev/null
  if [ $? -eq 0 ]; then
    sed -i -e '$!N;/\n.*Path not available for DELETE:/!P;D'  /home/print3D/moonraker/moonraker/components/file_manager/file_manager.py
    sed -i "/Path not available for DELETE:/c\                self.full_access_roots.add\(\'config\'\)\n                self.full_access_roots.add\(\'gcodes\'\)" /home
  fi
  grep "Invalid root request:" /home/print3D/moonraker/moonraker/components/file_manager/file_manager.py > /dev/null
  if [ $? -eq 0 ]; then
    sed -i "/Invalid root request:/c\                    self.full_access_roots.add\(\'config\'\)\n                    self.full_access_roots.add\(\'gcodes\'\)" /home/
  fi